package demo.java.java8.Lambdas.demo6;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import demo.java.java8.Lambdas.demo4.Dish;
import demo.java.java8.Lambdas.demo4.Dish.CaloricLevel;
import demo.java.java8.Lambdas.demo5.Test;

public class CollectorGroup {

	public static void main(String[] args) {
//		//按照类型分组
//		Map<Dish.Type, List<Dish>> dishesByType=Test.menu.stream()
//				.collect(Collectors.groupingBy(Dish::getType));
//		System.out.println(dishesByType);
//		Map<Dish.CaloricLevel, List<Dish>> dishesByLevel=Test.menu.stream().collect(
//				Collectors.groupingBy(dish -> {
//					if(dish.getCalories()<=400){
//						return CaloricLevel.DIET;
//					}else if(dish.getCalories()<=700){
//						return CaloricLevel.NORMAL;
//					}else{
//						return CaloricLevel.FAT;
//					}
//				}));
//		System.out.println(dishesByLevel);
//		
//		//多级分组
//		Map<Dish.Type,Map<Dish.CaloricLevel,List<Dish>>> dishesByTypeCaloricLevel=
//				Test.menu.stream().collect(
//						Collectors.groupingBy(Dish::getType,
//								Collectors.groupingBy(dish ->  {
//									if(dish.getCalories()<=400){
//										return CaloricLevel.DIET;
//									}else if(dish.getCalories()<=700){
//										return CaloricLevel.NORMAL;
//									}else{
//										return CaloricLevel.FAT;
//									}
//								})
//						)
//				);
//		System.out.println(dishesByTypeCaloricLevel);
		
		//查找每个子组热量最高的Dish
		Map<Dish.Type,Dish> mostCaloricByType=Test.menu.stream()
				.collect(Collectors.groupingBy(Dish::getType,
						Collectors.collectingAndThen(
								Collectors.maxBy(Comparator.comparingInt(Dish::getCalories))
								, Optional::get)
						));
		System.out.println(mostCaloricByType);
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}
